const { Paragraph, TextRun, CheckBox } = require("docx");
const { getAlignment, getIndentValue, generateUnderlineText } = require("../common/utils");

function generateRadioWithInput(field, baseIndent = 0) {
    const { value } = field;
    const elements = [];
    const finalIndent = getIndentValue(value.alignment, baseIndent);
    
    // 渲染选项
    value.options.forEach(option => {
        elements.push(
            new Paragraph({
                indent: { left: finalIndent },
                alignment: getAlignment(value.alignment),
                spacing: { before: 50, after: option.showInput ? 0 : 50 },
                children: [
                    new CheckBox({
                        checked: option.value === value.selected,
                        size: 24
                    }),
                    new TextRun({
                        text: ' ' + option.label,
                        size: 21, // 5号字体
                        font: "SimSun" // 宋体
                    })
                ]
            })
        );
        
        // 如果选项有输入框，始终显示输入区域
        if (option.showInput) {
            let inputText = '';
            if (option.value === value.selected && value.inputValue) {
                // 如果选中且有输入值，显示输入值
                inputText = `${option.inputLabel || ''}：${value.inputValue}`;
            } else {
                // 否则显示横线作为占位符
                inputText = `${option.inputLabel || ''}：${'_'.repeat(20)}`;
            }
            
            elements.push(
                new Paragraph({
                    indent: { left: finalIndent + 360 }, // 额外缩进
                    alignment: getAlignment(value.alignment),
                    children: [
                        new TextRun({
                            text: inputText,
                            size: 24
                        })
                    ]
                })
            );
        }
    });
    
    return elements;
}



module.exports = {
    generateRadioWithInput
}; 